<?php
/**
 * 留言板管理
 */
class MessageApp extends CommonApp {
    
    //发送留言
    public function sendAct(){
    	// 判断用户是否登录
    	//$this->checkLogin();//TODO:测试内容
        $member_id = My::arr2var($_SESSION,'member.id');
        $member_id = 1;//TODO:测试内容
        //用户信息
        $member = My::id2detail('member',$member_id);
        $this->assign('member', $member);
        if($_POST){
            $data['item'] = My::arr2var($_POST, 'item','feedback');
            $data['content'] = My::arr2var($_POST, 'content');
            $data['tel'] = My::arr2var($_POST, 'tel');
            $data['email'] = My::arr2var($_POST, 'email');
            if(empty($data['content']))
            	$this->ajaxReturn(300, '留言内容不能为空!');
            if(mb_strlen($data['content'],'UTF-8')>3000)
                $this->ajaxReturn(300, '内容不能超过3千个字!');
           	if(!empty($data['tel']) and !My::is_tel($data['tel']))
           		$this->ajaxReturn(300, '请输入有效的手机号!');
           	if(!empty($data['email']) and !My::is_email($data['email']))
           		$this->ajaxReturn(300, '请输入有效的邮箱地址!');
            $verify = new Verify();
            if(!$verify->check(My::arr2var($_POST,'verify'))){
            	$this->ajaxReturn(300, '验证码有误!');
            }
        	$data['member_id'] = $member_id;
        	$data['adddate'] = date("Y-m-d H:i:s");
        	$data['ip'] = get_client_ip();
            $ok = DB('message') ->clear()->add($data);
	        if($ok>0){
	            self::sendEmail($data);
	            $this->ajaxReturn(200, '发送成功!');
	        }else
	            $this->ajaxReturn(300, '发送失败!');
        }
        //SEO
        $result['params']['seo_title']=  '留言反馈';
        $this->assign('result',$result);
        //display
        $this->display('',$_GET);
    }  
    
    
    //我的留言
    public function indexAct(){
        //判断是否已登录,如未登录则跳转到登录页面
        //$this->checkLogin();//TODO:测试内容
        $member_id = My::arr2var($_SESSION,'member.id');
        $member_id = 1;//TODO:测试内容
        //最新20个留言
        $message =  DB('message')->where("member_id='{$member_id}' AND item!='reply'")->order('id DESC')->limit(0,20)->cache()->select();
        if($message){
            foreach($message as $key=>&$value){
                $value['reply'] = DB('message')->where("item='reply' AND idid='{$value['id']}'")->order('id DESC')->cache()->find();
            }
        }
        if(!$message)
            redirect(Router::encodeUrl('index.php?app=message&act=send'));
        $this->assign('message',$message);
        //SEO
        $result['params']['seo_title']=  '我的留言';
        $this->assign('result',$result);
        //display
        $this->display('',$_GET);
    }
    
    //发送邮件
    private function sendEmail($data){
        $message_type = C('message_type');
        $member = My::id2detail('member',$data['member_id']);
        $emailParam['title'] = '用户留言【'.$message_type[$data['item']].'】';
        $email_content = "用户信息：".(empty($member['nickname']) ? $member['username'] : $member['nickname'])."（UID：{$member['id']}）";
        if($data['tel'])
            $email_content.="<br/>留言者手机号：".$data['tel'];
        if($data['email'])
            $email_content.="<br/>留言者邮箱：".$data['email'];
        $email_content.="<br/>留言内容：".$data['content'];
        $emailParam['content'] = $email_content;
        $receiver =explode(",",My::alias2field('config','system_config','params.email'));
        if($receiver){
            foreach ($receiver as $key=>&$value) {
                $value = trim($value);
                if(!My::is_email($value)) unset($receiver[$key]);
            }
        }
        if(!$receiver)
            return false;
        $emailParam['receiver'] = $receiver;
        $sendok = My::sendApiMail($emailParam);
        return true;
    }
}